from .dsdiffdef import * 
from .dsdreusetags import GetChunkInfo

def GetId3FrameInfo(chunk):
    ivar = struct.unpack(">IIH", chunk)
    return ivar

def GetId3Tag(id3Bytes):
    album = ""
    artist = ""
    title = ""

    start = ID3TagHeaderSize
    end = ID3TagHeaderSize+ID3FrameHeaderSize
    chunk=id3Bytes[start:end]

    ivar = GetId3FrameInfo(chunk)
    while (ivar != ""):
        id = ivar[0].to_bytes(4, byteorder="big", signed=False)
        strStart = start+ID3FrameHeaderSize
        strEnd = strStart + int(ivar[1])
        print(id)
        if(id==b'TALB'):
            album = id3Bytes[strStart:strEnd]
            print(album)
        if(id==b'TIT2'):
            title = id3Bytes[strStart:strEnd]
            print(title)
        if(id==b'TPE2'):
            artist = id3Bytes[strStart:strEnd]
            print(artist)
        if(id==b'TPE1'):
            artist = id3Bytes[strStart:strEnd]
            print(artist)

        start = end+ivar[1]
        end = start+ID3FrameHeaderSize
        if(end > len(id3Bytes)):
            ivar = ""
        else:
            chunk=id3Bytes[start:end]            
            ivar = GetId3FrameInfo(chunk)
    return album, artist, title

def get_dsd_tag(fin):

    album = ""
    artist = ""
    title = ""

    with open(fin, mode="rb") as f:
        ivar = GetChunkInfo(f)
        totalLen = ivar[1]
        while (ivar != ""):
            id = ivar[0].to_bytes(4, byteorder="big", signed=False)
            if(id == b'FRM8'):
                chunk = f.read(4)
                if(chunk != b"DSD "):
                    print("DSDFormChunk error!", chunk)
                    return
            elif(id == b'FVER' or id == b'DSD ' or id == b'COMT'):
                f.seek(ivar[1],1)
            elif(id == b'PROP' or id == b'DIIN'):
                chunk=f.read(ivar[1])
                print(id, " bytes start:")
                print(chunk)
                print(id, " bytes end:")                
            elif(id == b'ID3 '):
                chunk=f.read(ivar[1])
                print(id, " bytes start:")
                album, artist, title = GetId3Tag(chunk)
                print(id, " bytes end:")
            if((f.tell()+12) < totalLen):
                ivar = GetChunkInfo(f)                    
            else:
                ivar = ""
        f.close()
    return album, artist, title


def get_dsd_singleTag(fin):

    singleTag = {}

    with open(fin, mode="rb") as f:
        ivar = GetChunkInfo(f)
        totalLen = ivar[1]
        while (ivar != ""):
            id = ivar[0].to_bytes(4, byteorder="big", signed=False)
            if(id == b'FRM8'):
                chunk = f.read(4)
                if(chunk != b"DSD "):
                    print("DSDFormChunk error!", chunk)
                    return
            elif(id == b'FVER' or id == b'DSD ' or id == b'COMT'):
                f.seek(ivar[1],1)
            elif(id == b'PROP' or id == b'DIIN'):
                chunk=f.read(ivar[1])
                print(id, " bytes start:")
                print(chunk)
                print(id, " bytes end:")                
            elif(id == b'ID3 '):
                chunk=f.read(ivar[1])
                print(id, " bytes start:")
                singleTag = GetId3SingleTag(chunk)
                print(id, " bytes end:")
            elif(id == b'DST  '):
                chunk=f.read(ivar[1])
                print(id, " bytes ignore: ", ivar[1])
                f.seek(ivar[1], 1)
            else:
                print(id, " bytes ignore: ", ivar[1])
                f.seek(ivar[1], 1)
            if((f.tell()+12) < totalLen):
                ivar = GetChunkInfo(f)                    
            else:
                ivar = ""
        f.close()
    return singleTag

def GetId3SingleTag(id3Bytes):

    singleTag = {}
    singleTag[b'TALB'] = ""
    singleTag[b'TIT2'] = ""
    singleTag[b'TPE2'] = ""
    singleTag[b'TPE1'] = ""
    singleTag[b'TRCK'] = ""
    singleTag[b'TYER'] = ""

    start = ID3TagHeaderSize
    end = ID3TagHeaderSize+ID3FrameHeaderSize
    chunk=id3Bytes[start:end]

    ivar = GetId3FrameInfo(chunk)
    while (ivar != ""):
        id = ivar[0].to_bytes(4, byteorder="big", signed=False)
        strStart = start+ID3FrameHeaderSize
        strEnd = strStart + int(ivar[1])
        print(id)
        if(id==b'TALB'):
            album = id3Bytes[strStart:strEnd]
            singleTag[b'TALB'] = album
            print(album)
        if(id==b'TIT2'):
            title = id3Bytes[strStart:strEnd]
            singleTag[b'TIT2'] = title
            print(title)
        if(id==b'TPE2'):
            artist = id3Bytes[strStart:strEnd]
            singleTag[b'TPE2'] = artist
            print(artist)
        if(id==b'TPE1'):
            artist = id3Bytes[strStart:strEnd]
            singleTag[b'TPE1'] = artist
            print(artist)
        if(id==b'TRCK'):
            track = id3Bytes[strStart:strEnd]
            singleTag[b'TRCK'] = track
            print(track)
        if(id==b'TYER'):
            year = id3Bytes[strStart:strEnd]
            singleTag[b'TYER'] = year
            print(year)

        start = end+ivar[1]
        end = start+ID3FrameHeaderSize
        if(end > len(id3Bytes)):
            ivar = ""
        else:
            chunk=id3Bytes[start:end]            
            ivar = GetId3FrameInfo(chunk)

    return singleTag